package org.ymjava.medical.wx.web;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.ymjava.medical.core.util.ResponseUtil;
import org.ymjava.medical.db.AllPackage.biz.MobileUserBiz;
import org.ymjava.medical.db.AllPackage.entity.MobileUser;
import org.ymjava.medical.wx.config.Configure;
import org.ymjava.medical.wx.config.HttpsUtil;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/wx_answer/wx_user")
public class MobileUserController {
    private final Log logger = LogFactory.getLog(MobileUserController.class);

    @Autowired
    MobileUserBiz mobileUserBiz;

    @GetMapping("/login")
    public Object Login(String code){
        JSONObject jsonObject = null;
        JSONObject userinfojsonObject = null;
        logger.info(code);
        String openid = "";
        String nickname = "";
        String avatar = "";
        Integer sex = 0;
        String access_token = "";
        String resultStr = "";
        String userinforesultStr = "";
        String requesUrl = Configure.WX_GET_ACCESS_URL;
        String userinfoUrl = Configure.WX_GET_USER_INFO_URL;
        Map<String,Object> data = new HashMap<>(2);
        try{
            if(!StringUtils.isBlank(code)&&!StringUtils.isBlank(requesUrl)){
                //开始获取用户openid
                requesUrl = requesUrl.replace("APP_ID",Configure.APP_ID).replace("APP_SECRET",Configure.APP_SECRET).replace("USE_CODE",code);
                //调用获取openid接口
                resultStr = HttpsUtil.httpsRequest(requesUrl,"GET",null);
                logger.info(resultStr);
                if(!StringUtils.isBlank(resultStr)&&resultStr.indexOf("openid")>0){
                    jsonObject = JSONObject.parseObject(resultStr);
                    System.out.println(jsonObject);
                    openid=jsonObject.get("openid").toString();
                    access_token =jsonObject.get("access_token").toString();
                    userinfoUrl = userinfoUrl.replace("ACCESS_TOKEN",access_token).replace("OPEN_ID",openid);
                    userinforesultStr = HttpsUtil.httpsRequest(userinfoUrl, "GET", null);
                    if(!StringUtils.isBlank(userinforesultStr)){
                        userinfojsonObject = JSONObject.parseObject(userinforesultStr);
                        System.out.println(userinfojsonObject);
                        avatar=userinfojsonObject.get("headimgurl").toString();
                        nickname=userinfojsonObject.get("nickname").toString();
                        sex = Integer.parseInt(userinfojsonObject.get("sex").toString());
                    }else{
                        return ResponseUtil.fail();
                    }
                }else{
                    return ResponseUtil.fail();
                }
            }
        }catch (Exception e){
            System.out.println(e);
            logger.info(e);
            return ResponseUtil.fail();
        }

        logger.info(avatar);
        logger.info(nickname);
        logger.info(sex);
        logger.info(openid);


        MobileUser user = mobileUserBiz.queryByOpenid(openid);
        logger.info(user);
        if(user == null){
            MobileUser muser = new MobileUser();
            muser.setWxOpenid(openid);
            muser.setDeleted(0);
            muser.setAddTime(new Date());
            muser.setAvatar(avatar);
            muser.setStatus(0);
            muser.setNickName(nickname);
            mobileUserBiz.insert(muser);
            data.put("userInfo", muser);
            data.put("openid", openid);
            return ResponseUtil.ok(data);
        }else{
            data.put("userInfo", user);
            data.put("openid", openid);
            return ResponseUtil.ok(data);
        }
//       return ResponseUtil.ok();
    }


    @PostMapping("/update_information")
    public Object update_informatiion(@RequestBody String body){

        logger.info(body);

        return ResponseUtil.ok();
    }

}
